package school.support.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JSONUtils {

    public static Map<String, Object> jsonToMap(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, Map.class);
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 将对象转换为json
     *
     * @param obj
     * @return
     * @Author 执笔
     * @Date 2016年10月26日下午3:17:11
     */
    public static String objToJson(Object obj) {
        if (null == obj) return null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    /**
     * 对象转换成map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> objToMap(Object object) {
        if (object == null) return new HashMap<>();
        String json = objToJson(object);
        return jsonToMap(json);
    }
}
